#include "pig.h"
#include "Scene.h"
#include "yumi.h"
#include "ThorSceneManager.h"

extern OgreCanvas * gc;
void Pig::spriteMove(int _xx, int _yy )
{
	/*Sprite::spriteMove(_xx,_yy);

	Ogre::Vector2 p2D = Ogre::Vector2(Ogre::Real(_xx), Ogre::Real(_yy));
	Ogre::Vector3 p3D = _getInteractPosition3D(m_pCamera,p2D,0);
	Ogre::Vector3 Destination= p3D;*/



}

void Pig::update(Ogre::Real timeSinceLast)
{
	try
	{


		switch(mState)
		{
		case PIG_RECOVERING:
			{
				if(recoveringProgress <= 1.0f)
				{
					recoveringProgress += recoveringFactor;
					Ogre::Quaternion delta = Ogre::Quaternion::Slerp(recoveringProgress,
						recoveringP, recoveringQ, true);//shortest path;
					setOrientation(delta);
				}
				
				if(m_pAnimationMixState->getCurrentDestAS()->getAnimationName() == "xiaozhu_standup")
				{
					Ogre::Real currentMotionTime = m_pAnimationMixState->getCurrentDestAS()->getTimePosition();
					if(currentMotionTime == m_pAnimationMixState->getCurrentDestAS()->getLength())
					{
						mState = STANDING;
					}
				}
			}
			break;
		case  PIG_EATING:
			{
				if(m_pAnimationMixState->getCurrentDestAS()->getAnimationName() == "xiaozhu_eat")
				{
					if(m_pAnimationMixState->hasPlayedOnce() &&m_pAnimationMixState->getLoopTime()>=2)
					{
						mState = STANDING;
					}
				}
			}
			break;
		case PIG_BEATEN:
			{
				if(beatenPeriod >= 0.0f)
				{
					beatenPeriod -= timeSinceLast;
				}
				else
				{
					mState = PIG_RECOVERING;
					beatenPeriod = 0.0f;
					setFXDisabled();
					setNeedSynchronize(false);
					setEnableCollision(false);

					//for recovering state
					recoveringP = getOrientation();
					recoveringQ = Ogre::Quaternion::IDENTITY;
					recoveringProgress = 0;
					
				}
			}
			break;
		default:
			{

				//get yumi node name first;
				Ogre::String _yumiName = Ogre::String("yumi1");
				//get yumi node
				Ogre::Node * _yumi = m_pSceneMgr->getRootSceneNode()->getChild(_yumiName);

				for(;!mDestinationList.empty();)
					mDestinationList.pop();

				Ogre::Vector3 _p = _yumi->getPosition();
				mDestination = Ogre::Vector3(_p.x, 0, _p.z);
				mAngularDestination = 
					-(mPosition - mDestination);

				Yumi * _sprite = (Yumi*)(ThorSceneManager::getInstance()->getCurrentScene()->getNPCSprite(_yumiName));

				//if the distance between them is less then the threshold
				//kill yumi, and stop pig's movement
				Ogre::Vector3 distance = _p - mPosition;

				if(distance.squaredLength() <= predatorDistance2)
				{
					if( _sprite->isAlive())
					{

						mDestination = mPosition;
					}
				}

				//if yumi has no life
				if(!(_sprite->isAlive()))
				{
					mDestination = mPosition;
				}

				Sprite::update(timeSinceLast);

				//change back motion after super's update
				if(distance.squaredLength() <= predatorDistance2)
				{
					if( _sprite->isAlive())
					{
						mState = PIG_EATING;
						_sprite->kill();
					}
				}

			}

		}
	}catch(Ogre::Exception e){}
	animationChanging(timeSinceLast);
}

void Pig::animationChanging(Ogre::Real timeSinceLast)
{
	//for addTime
	Sprite::animationChanging(timeSinceLast);



	////////////////////////---------------------animation changing
	if((mState & STANDING) != 0)
	{
		Ogre::String currentAS = m_pAnimationMixState->getCurrentDestAS()->getAnimationName();
		if(currentAS == "xiaozhu_stand")
			return;
		m_pAnimationMixState->changeAnimation("xiaozhu_stand");
	}
	else if((mState & STRAIGHT_RUNING) != 0)
	{
		Ogre::String currentAS = m_pAnimationMixState->getCurrentDestAS()->getAnimationName();
		if(currentAS == "xiaozhu_run")
			return;
		m_pAnimationMixState->changeAnimation("xiaozhu_run");
	}
	else if((mState & TURNING) != 0)
	{
		Ogre::String currentAS = m_pAnimationMixState->getCurrentDestAS()->getAnimationName();
		if(currentAS == "xiaozhu_run")
			return;
		m_pAnimationMixState->changeAnimation("xiaozhu_run");
	}
	else if(mState  == PIG_EATING)
	{
		Ogre::String currentAS = m_pAnimationMixState->getCurrentDestAS()->getAnimationName();
		if(currentAS == "xiaozhu_eat")
			return;
		m_pAnimationMixState->changeAnimation("xiaozhu_eat",true);
	}
	else if(mState  == PIG_BEATEN)
	{
		Ogre::String currentAS = m_pAnimationMixState->getCurrentDestAS()->getAnimationName();
		if(currentAS == "pig_knock")
			return;
		m_pAnimationMixState->changeAnimation("pig_knock",true);
	}
	else if(mState  == PIG_RECOVERING)
	{
		Ogre::String currentAS = m_pAnimationMixState->getCurrentDestAS()->getAnimationName();
		if(currentAS == "xiaozhu_standup")
			return;
		m_pAnimationMixState->getAnimationState("xiaozhu_standup")->setTimePosition(0.0f);
		m_pAnimationMixState->changeAnimation("xiaozhu_standup",false);
	}


}

void Pig::setFXVolumn()
{
	m_pDynamicsWorld->removeRigidBody(m_pRigidBody);
	//delete or not, not sure yet, might mem leak
	//delete m_pRigidBody,m_pMotionState;
	delete m_pShape;

	m_pShape = new btCapsuleShapeZ(1.2791f/4.f, 2.0f/4.f);
	//((btCapsuleShapeZ*)m_pShape)->setImplicitShapeDimensions(btVector3(0,100,0));
	m_pMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,-1.f/4.f,0)));
	m_mass = 1;
	btVector3 _pInertia(0,0,0);
	m_pShape->calculateLocalInertia(m_mass,_pInertia);
	btRigidBody::btRigidBodyConstructionInfo _RigidBodyCI(m_mass,m_pMotionState,m_pShape,_pInertia);
	m_pRigidBody = new btRigidBody(_RigidBodyCI);
	m_pDynamicsWorld->addRigidBody(m_pRigidBody);
	m_pRigidBody->setMotionState(m_pMotionState);

	mFXCreated = FX_CREATED;

}

void Pig::beaten(Ogre::Vector3 Impluse)
{
	mState = PIG_BEATEN;
	beatenPeriod = BEATEN_PERIOD;
	setFXEnabled();
	setNeedSynchronize(true);
	setEnableCollision(true);
	setPosition(m_pSpriteNode->getPosition() + Ogre::Vector3(0,0.1f/4.f,0));// to prevent merge into ground
	setOrientation(mOrientation);
	m_pRigidBody->setLinearVelocity(btVector3(0.0f,0.0f,0.0f));
	m_pRigidBody->setAngularVelocity(btVector3(0.0f,0.0f,0.0f));
	m_pRigidBody->applyImpulse(btVector3(Impluse.x,Impluse.y,Impluse.z),btVector3(0,-0.1,0));//,btVector3(ImpluseFrom.x,ImpluseFrom.y,ImpluseFrom.z)
}